package com.LeeCode;

/**
 * 考试的最大困扰度
 */

public class Code2024 {
    public static void main(String[] args) {
        String answerKey = "TFFT";
        int k = 1;

        System.out.println(new Code2024().maxConsecutiveAnswers(answerKey, k));
    }

    public int maxConsecutiveAnswers(String answerKey, int k) {
        int ans = 0, left = 0;
        char[] chars = answerKey.toCharArray();

        int[] cnt = new int[2];

        for (int i = 0; i < answerKey.length(); i++) {
            if (chars[i] == 'T')
                cnt[0]++;
            else if (chars[i] == 'F')
                cnt[1]++;

            while (cnt[0] > k && cnt[1] > k) {
                if (chars[left] == 'T')
                    cnt[0]--;
                else if (chars[left] == 'F')
                    cnt[1]--;
                left++;
            }
            ans = Math.max(ans, i - left + 1);
        }

        return ans;
    }
}
